//
//  TestArrayTransmitVC.m
//  技术点总结
//
//  Created by tianlong on 2018/6/8.
//  Copyright © 2018年 tianlong. All rights reserved.
//

#import "TestArrayTransmitVC.h"
#import "Person.h"
#import "NSArray+Extension.h"

@interface TestArrayTransmitVC ()
@property (strong, nonatomic) NSArray *arr;
@property (copy, nonatomic) id arr1;
@property (copy, nonatomic) id arr2;
@property (copy, nonatomic) id arr3;
@property (copy, nonatomic) id arr4;
@property (copy, nonatomic) id arr5;
@property (copy, nonatomic) id arr6;

@property (strong, nonatomic) id arr1_strong;
@property (strong, nonatomic) id arr2_strong;
@property (strong, nonatomic) id arr3_strong;
@property (strong, nonatomic) id arr4_strong;
@property (strong, nonatomic) id arr5_strong;
@property (strong, nonatomic) id arr6_strong;

@end

@implementation TestArrayTransmitVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    Person *p1 = [[Person alloc] init];
    p1.name = @"张三";
    p1.dog = [[Dog alloc] init];
    p1.dog.name = @"旺财";
    p1.dog.food = [[Food alloc] init];
    p1.dog.food.name = @"旺仔小馒头";
    
    Person *p2 = [[Person alloc] init];
    p2.name = @"2222";
    p2.dog = [[Dog alloc] init];
    p2.dog.name = @"二哈";
    
    
    self.arr = [NSArray arrayWithObjects:p1,p2,nil];
//    self.arr = @[p1,p2];
    
    /**
     以上两种创建数组的方式略有不同
     */
}

#pragma mark - 不可变
- (IBAction)testImmutable:(id)sender {
    
    self.arr1 = self.arr;
    self.arr2 = [self.arr copy];
    self.arr3 = [self.arr mutableCopy];
    self.arr4 = [NSArray arrayWithArray:self.arr];
    self.arr5 = [[NSArray alloc] initWithArray:self.arr];
    self.arr6 = [[NSArray alloc] initWithArray:self.arr copyItems:YES];
    self.arr6 = [NSArray deepCopyFromArray:self.arr];

    NSLog(@"arr[0]  = %@ arr_add  = %p arr_class  = %@",self.arr[0],self.arr,NSStringFromClass([self.arr class]));
    NSLog(@"arr1[0] = %@ arr1_add = %p arr1_class = %@",self.arr1[0],self.arr1,NSStringFromClass([self.arr1 class]));
    NSLog(@"arr2[0] = %@ arr2_add = %p arr2_class = %@",self.arr2[0],self.arr2,NSStringFromClass([self.arr2 class]));
    NSLog(@"arr3[0] = %@ arr3_add = %p arr3_class = %@",self.arr3[0],self.arr3,NSStringFromClass([self.arr3 class]));
    NSLog(@"arr4[0] = %@ arr4_add = %p arr4_class = %@",self.arr4[0],self.arr4,NSStringFromClass([self.arr4 class]));
    NSLog(@"arr5[0] = %@ arr5_add = %p arr5_class = %@",self.arr5[0],self.arr5,NSStringFromClass([self.arr5 class]));
    NSLog(@"arr6[0] = %@ arr6_add = %p arr6_class = %@",self.arr6[0],self.arr6,NSStringFromClass([self.arr6 class]));
    
    /**
     arr[0]  = <Person: 0x60000000daf0> arr_add  = 0x60000003b840 arr_class  = __NSArrayI
     
     arr1[0] = <Person: 0x60000000daf0> arr1_add = 0x60000003b840 arr1_class = __NSArrayI
     arr2[0] = <Person: 0x60000000daf0> arr2_add = 0x60000003b840 arr2_class = __NSArrayI
     
     arr3[0] = <Person: 0x60000000daf0> arr3_add = 0x600000433780 arr3_class = __NSArrayI
     arr4[0] = <Person: 0x60000000daf0> arr4_add = 0x600000429ac0 arr4_class = __NSArrayI
     arr5[0] = <Person: 0x60000000daf0> arr5_add = 0x600000432300 arr5_class = __NSArrayI
     
     arr6[0] = (null) arr6_add = 0x0 arr6_class = (null)

     结论
     copy修饰的数组
     等号与copy 对数组：浅拷贝，指向的内存区域与被拷贝的数组是同一块内存
     其他几种操作对数组：深拷贝
     上述所有方式，对被拷贝数组中的元素都是浅拷贝
     */
}

#pragma mark - 可变
- (IBAction)testMutable:(id)sender {
    
    self.arr1_strong = self.arr;
    self.arr2_strong = [self.arr copy];
    self.arr3_strong = [self.arr mutableCopy];
    self.arr4_strong = [NSArray arrayWithArray:self.arr];
    self.arr5_strong = [[NSArray alloc] initWithArray:self.arr];
//    self.arr6_strong = [[NSArray alloc] initWithArray:self.arr copyItems:YES];
    self.arr6_strong = [NSArray deepCopyFromArray:self.arr];
    NSLog(@"arr[0]  = %@ arr_add  = %p arr_class  = %@",self.arr[0],self.arr,NSStringFromClass([self.arr class]));
    NSLog(@"arr1[0] = %@ arr1_add = %p arr1_class = %@",self.arr1_strong[0],self.arr1_strong,NSStringFromClass([self.arr1_strong class]));
    NSLog(@"arr2[0] = %@ arr2_add = %p arr2_class = %@",self.arr2_strong[0],self.arr2_strong,NSStringFromClass([self.arr2_strong class]));
    NSLog(@"arr3[0] = %@ arr3_add = %p arr3_class = %@",self.arr3_strong[0],self.arr3_strong,NSStringFromClass([self.arr3_strong class]));
    NSLog(@"arr4[0] = %@ arr4_add = %p arr4_class = %@",self.arr4_strong[0],self.arr4_strong,NSStringFromClass([self.arr4_strong class]));
    NSLog(@"arr5[0] = %@ arr5_add = %p arr5_class = %@",self.arr5_strong[0],self.arr5_strong,NSStringFromClass([self.arr5_strong class]));
    NSLog(@"arr6[0] = %@ arr6_add = %p arr6_class = %@",self.arr6_strong[0],self.arr6_strong,NSStringFromClass([self.arr6_strong class]));
    
    /**
     arr[0]  = <Person: 0x600000010950> arr_add  = 0x60000023e520 arr_class  = __NSArrayI
     
     arr1[0] = <Person: 0x600000010950> arr1_add = 0x60000023e520 arr1_class = __NSArrayI
     arr2[0] = <Person: 0x600000010950> arr2_add = 0x60000023e520 arr2_class = __NSArrayI
     
     arr3[0] = <Person: 0x600000010950> arr3_add = 0x60400045d400 arr3_class = __NSArrayM
     
     arr4[0] = <Person: 0x600000010950> arr4_add = 0x60400042e140 arr4_class = __NSArrayI
     arr5[0] = <Person: 0x600000010950> arr5_add = 0x60400042fca0 arr5_class = __NSArrayI
     
     arr6[0] = (null) arr6_add = 0x0 arr6_class = (null)
     
     结论
     strong修饰的数组
     等号与copy 对数组：浅拷贝，指向的内存区域与被拷贝的数组是同一块内存
     其他几种操作对数组：深拷贝
     上述所有方式，对被拷贝数组中的元素都是浅拷贝
     */
}

- (IBAction)changeFristPersonName:(id)sender{
    
    
    Person *p0 = self.arr[0];
    Person *p6 = self.arr6[0];
    NSLog(@"更改前：arr[0].name = %@ arr[0].dog.name = %@ arr[0].dog.food.name = %@",p0.name,p0.dog.name,p0.dog.food.name);
    NSLog(@"更改前：arr6[0].name = %@ arr6[0].dog.name = %@ arr6[0].dog.food.name = %@",p6.name,p6.dog.name,p6.dog.food.name);
    
    //  更改arr1数组中第一个元素的相关属性的值
    Person *p1 = self.arr1[0];
    p1.name = @"李四";
    p1.dog.name = @"柯基";
    p1.dog.food.name = @"小短腿薯片";
    
    // 更改后
    NSLog(@"p1 更改后：arr[0].name = %@ arr[0].dog.name = %@ arr[0].dog.food.name = %@",p0.name,p0.dog.name,p0.dog.food.name);
    NSLog(@"p1 更改后：arr6[0].name = %@ arr6[0].dog.name = %@ arr6[0].dog.food.name = %@",p6.name,p6.dog.name,p6.dog.food.name);
    
    p6.name = @"王五";
    p6.dog.name = @"萨摩耶";
    p6.dog.food.name = @"奶油面包";

    // 更改后
    NSLog(@"p6 更改后：arr[0].name = %@ arr[0].dog.name = %@ arr[0].dog.food.name = %@",p0.name,p0.dog.name,p0.dog.food.name);
    NSLog(@"p6 更改后：arr6[0].name = %@ arr6[0].dog.name = %@ arr6[0].dog.food.name = %@",p6.name,p6.dog.name,p6.dog.food.name);
    
}

@end
